<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="wifi" :model="wifiData" labelWidth="90rpx">
				<uni-forms-item label="名称">
					<uni-easyinput v-model="wifiData.ssid" placeholder="请输入WiFi名称"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item label="密码">
					<uni-easyinput v-model="wifiData.password" placeholder="请输入WiFi密码"
						style="margin-top: 15rpx;"></uni-easyinput>
				</uni-forms-item>
			</uni-forms>
			<uni-section title="WIFI列表" type="line" padding>
				<uni-list>
					<uni-list-item v-for = "item in existingWifi" :key = item.SSID :title = item.SSID
						rightText="WIFI需输入密码" clickable @click="onClick(item)">
					</uni-list-item>
				</uni-list>
			</uni-section>
			<button type="second" style="margin-top: 60rpx;" @tap="search('wifi')">{{buttonText}}</button>
			<button type="primary" style="margin-top: 30rpx;" @tap="send('wifi')">提交</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				wifiData: {
					ssid: '',
					password: '',
				},
				deviceUUID: '',
				existingWifi: [], // wifi列表
				buttonText: '搜索wifi',
			}
		},
		onLoad(options) {
			if (options.uuid) {
				console.log('wifi页面收到uuid', options.uuid);
				this.deviceUUID = options.uuid;
			} else {
				console.log('wifi页面没有收到uuid');
			}
			uni.startWifi({});
		},
		methods: {
			async search(ref) {
				this.buttonText = 'WiFi获取中 ...';
				this.searchWifi();
			},
			async searchWifi() {
				const systemInfo = uni.getSystemInfoSync();
				let platform;
				if (systemInfo.platform === 'android') {
					await uni.getWifiList({
						success: (res) => {
							uni.onGetWifiList((res) => {
								const uniqueWifiList = this.removeDuplicates(res.wifiList);
								this.existingWifi = uniqueWifiList;
								console.log('去重后的WiFi列表:', this.existingWifi);
							});
						},
						fail: (err) => {console.error("获取wifi列表失败", err);},
					});
				} else if (systemInfo.platform === 'ios') {
					// ios需要Access WiFi information”;
				}
				setTimeout(() => {
					this.buttonText = '搜索wifi';
				}, 10000);
			},
			removeDuplicates(wifiList) {
				const seen = new Set();
				return wifiList.filter(item => {
					const duplicate = seen.has(item.SSID);
					seen.add(item.SSID);
					return !duplicate;
				});
			},
			onClick(item) {
				this.wifiData.ssid = item.SSID;
			},
			send(ref) {
				let message = {
					msg: "wifi_config",
					data: Array(5).fill({
						ssid: "NULL",
						password: "NULL"
					})
				};
				message.data[0] = {
					ssid: this.wifiData.ssid,
					password: this.wifiData.password
				};

				let opts = {
					topic: 'LSY/app/' + this.deviceUUID,
					message: JSON.stringify(message)
				};

				this.$mqttTool.publish(opts).then(res => {
					console.log(opts);
					uni.showToast({
						title: '消息发送成功',
						icon: 'success',
						duration: 2000
					});
				}).catch(err => {
					uni.showToast({
						title: '消息发送失败',
						icon: 'none',
						duration: 2000
					});
				});
			}
		}
	}
</script>

<style lang="scss">
	.container {
		display: flex;
		flex-direction: column;
		align-items: stretch;
	}

	.example {
		padding: 15px;
		background-color: #fff;
		height: auto;
	}
</style>